import React, { useEffect, useState } from "react";
import { useNavigate } from "react-router-dom";

import { login } from "../services/user";
import { Button, Card, Form, Input, Modal } from "antd";

function Login(props) {
    const navigate = useNavigate();
    const onFinish = async (values) => {
        let data = await login(values.username, values.pwd);
        if (data.status == 1) {
            localStorage.setItem("token",data.token);
            navigate("/manage");
        } else {
            Modal.error({
                content: "登录失败",
            });
        }

        // console.log("Success:", values);
    };
    const onFinishFailed = (errorInfo) => {
        console.log("Failed:", errorInfo);
    };

    return (
        <Card
            title="登录"
            style={{
                width: 600,
                margin: "auto",
            }}
        >
            <Form
                name="basic"
                labelCol={{
                    span: 8,
                }}
                wrapperCol={{
                    span: 16,
                }}
                initialValues={{
                    remember: true,
                }}
                onFinish={onFinish}
                onFinishFailed={onFinishFailed}
                autoComplete="off"
            >
                <Form.Item
                    label="用户名"
                    name="username"
                    hasFeedback
                    rules={[
                        {
                            required: true,
                            message: "请输入用户名",
                        },
                    ]}
                >
                    <Input />
                </Form.Item>

                <Form.Item
                    label="密码"
                    name="pwd"
                    rules={[
                        {
                            required: true,
                            message: "请输入密码",
                        },
                    ]}
                >
                    <Input.Password />
                </Form.Item>

                <Form.Item
                    wrapperCol={{
                        offset: 8,
                        span: 16,
                    }}
                >
                    <Button type="primary" htmlType="submit">
                        Submit
                    </Button>
                </Form.Item>
            </Form>
        </Card>
    );
}
export default Login;
